package com.hy.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.bean.PageBean;
import com.hy.bean.UmsUser;
import com.hy.dao.UserDao;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class UserController {
    private UserDao userDao = new UserDao();
    private ObjectMapper objectMapper = new ObjectMapper();


    public PageBean<List<UmsUser>> queryFollowMe(HttpServletRequest request, HttpServletResponse response,Integer id){
        try {
            List<UmsUser> user = userDao.selectFollowMe(id);
            PageBean<List<UmsUser>> pb = new PageBean<>();

            pb.setCode(user.size());
            pb.setMsg("ok");
            pb.setStatus(200);
            pb.setData(user);
            return pb;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public PageBean<List<UmsUser>> queryFollowUesr(HttpServletRequest request, HttpServletResponse response,Integer id){
        try {
            List<UmsUser> user = userDao.selectFollowUesr(id);
            PageBean<List<UmsUser>> pb = new PageBean<>();

            pb.setCode(user.size());
            pb.setMsg("ok");
            pb.setStatus(200);
            pb.setData(user);
            return pb;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession(false);

        response.sendRedirect(request.getContextPath()+"/show.jsp");
    }
    public String geren(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession(false);

        response.sendRedirect(request.getContextPath()+"/Personal.jsp");
        return "200";
    }
    public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession(false);

        if(session != null){
            session.removeAttribute("username");
        }

        //退出后，重定向到登录页面
        response.sendRedirect(request.getContextPath()+"/login.jsp");
        return "200";
    }
    //注册前查询
    public PageBean<UmsUser> registerReview(HttpServletRequest request, HttpServletResponse response, String username) {
        try {
            UmsUser user = userDao.selectBy(username);
            PageBean<UmsUser> pb = new PageBean<>();
            if (user != null) {
                pb.setCode(000001);
                pb.setMsg("该账号已注册");
                pb.setStatus(500);
                pb.setData(user);
                return pb;
            }
            pb.setCode(000001);
            pb.setMsg("该账号未注册");
            pb.setStatus(200);
            pb.setData(user);
            return pb;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //注册
    public PageBean<UmsUser> register(HttpServletRequest request,HttpServletResponse response,String username,String password){
        try {
            boolean flag = userDao.addUser(username, password);
            PageBean<UmsUser> pb = new PageBean<>();
            if(flag){
                UmsUser user = new UmsUser(null,username, password,null,null,null,null,null,null,null,null,null,null,null);
                HttpSession session = request.getSession(true);
                session.setAttribute("username", username);
                session.setAttribute("password",password);

                pb.setCode(100000);
                pb.setMsg("注册加载到首页");
                pb.setStatus(200);
                pb.setData(user);
                return pb;
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //登录
    public PageBean<UmsUser> toHomePage(HttpServletRequest request,HttpServletResponse response,String username,String password){
        try {
            PageBean<UmsUser> pb = new PageBean<>();
            UmsUser user = userDao.selectBy(username);
            if(user == null || user.getPassword().equals(password) == false){
                System.out.println(user);
                System.out.println(password);
                pb.setMsg("加载失败！");
                pb.setStatus(500);
                pb.setData(user);
                pb.setCode(000100);
                return pb;
            }
            HttpSession session = request.getSession(true);

            session.setAttribute("username", username);
            session.setAttribute("password",password);
            session.setAttribute("id",user.getId());

            Cookie c1 = new Cookie("JSESSIONID", session.getId());
            c1.setMaxAge(60 * 20);

            response.addCookie(c1);

            pb.setCode(100000);
            pb.setMsg("加载到首页");
            pb.setStatus(200);
            pb.setData(user);
            return pb;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public PageBean<List<Map<String, Object>>> Attention(HttpServletRequest request,HttpServletResponse response,Integer id){
        try {
            List<Map<String, Object>> mapList = userDao.selectByAttention(id);
            PageBean<List<Map<String, Object>>> pb = new PageBean<>();
            if (mapList.size() == 0) {
                pb.setCode(000001);
                pb.setMsg("该账号已注册");
                pb.setStatus(500);
                pb.setData(mapList);
                return pb;
            }
            pb.setCode(000001);
            pb.setMsg("该账号未注册");
            pb.setStatus(200);
            pb.setData(mapList);
            return pb;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void test(Integer id){
        System.out.println(id);
    }
}
